from django import forms
from .. import models


class BookForm(forms.Form):
    title = forms.CharField(max_length=4)

    def __init__(self, instance=None, *args, **kwargs):
        self.instance = instance
        super(BookForm, self).__init__(*args, **kwargs)

    def save(self):
        if self.instance is not None:
            for key, value in self.cleaned_data.items():
                setattr(self.instance, key, value)
            self.instance.save()
            return self.instance
        return models.Book.objects.create(**self.cleaned_data)
